package com.dnjn.happiness.emo.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.api.RemoteEmotionTypeService;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.InnerAuth;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.assist.FerAssist;
import com.dnjn.happiness.emo.controller.common.CommonUserRecordFerController;
import com.dnjn.happiness.emo.domain.vo.UserRecordFerAddVO;
import com.dnjn.happiness.emo.domain.vo.UserRecordFerExportVo;
import com.dnjn.happiness.emo.domain.UserRecordFer;
import com.dnjn.happiness.emo.service.IUserRecordFerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * 用户AI情绪userRecordFerController
 *
 * @author amber
 * @date 2023-12-04
 */
@Tag(name = "emo admin userRecordFer 用户AI情绪userRecordFer", description = "用户AI情绪userRecordFer")
@RestController
@RequestMapping("/admin/userRecordFer")
public class AdminUserRecordFerController extends CommonUserRecordFerController {
    @Autowired
    private IUserRecordFerService userRecordFerService;
    @Autowired
    private RemoteEmotionTypeService emotionTypeService;

    @Autowired
    private FerAssist ferAssist;

    @InnerAuth
    @GetMapping("/hasClockInToday/{userId}")
    public R<Boolean> hasClockInToday(@PathVariable Long userId){
        Long count = userRecordFerService.lambdaQuery().eq(UserRecordFer::getUserId, userId).eq(UserRecordFer::getDeleted, "0").eq(UserRecordFer::getCurrDate, LocalDate.now()).count();
        return R.ok(count > 0);
    }

    @InnerAuth
    @GetMapping("/getAIHappinessScores/{userId}/{time}")
    public R<Double> getAIHappinessScores(@PathVariable Long userId,@PathVariable String time){
        return R.ok(userRecordFerService.getAIHappinessScores(userId,time));
    }

    @Log(title = "用户AI情绪userRecordFer", businessType = BusinessType.LIST)
    @RequiresPermissions("@ss.hasAnyPermi('emo:userRecordFer:list,ignore:userRecordFer:list')")
    @PostMapping("/list")
    public AjaxResult list(@Validated @RequestBody UserRecordFer vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {

        if(Objects.isNull(vo.getDeptId())){
            vo.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        }

        Page page = Page.of(current, size);
        Page<UserRecordFer> list = userRecordFerService.getPage(vo, page);
        return success(list);
    }

    /**
     * 导出用户AI情绪userRecordFer列表
     */
    @Log(title = "用户AI情绪userRecordFer", businessType = BusinessType.EXPORT)
    @RequiresPermissions("@ss.hasPermi('emo:userRecordFer:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserRecordFerAddVO vo) {
        UserRecordFer userRecordFer = new UserRecordFer();
        BeanUtils.copyPropsIgnoreNull(vo, userRecordFer);
        List<UserRecordFerExportVo> list = userRecordFerService.getExportList(userRecordFer);
        ExcelUtil<UserRecordFerExportVo> util = new ExcelUtil<UserRecordFerExportVo>(UserRecordFerExportVo.class);
        util.exportExcel(response, list, "用户AI情绪userRecordFer数据");
    }

    @Deprecated
    @Operation(summary = "后台-根据用户id查询")
    @RequiresPermissions("@ss.hasPermi('emo:userRecordFer:query')")
    @GetMapping(value = "/get1")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getByUserId1(@RequestParam("userId") Long userId, @RequestParam(required = false) String emotionCode, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<UserRecordFer> list = userRecordFerService.selectAi(userId, emotionCode, startTime, endTime, page);


        return success(list);
    }

    @RequiresPermissions("@ss.hasPermi('emo:userRecordFer:query')")
    @GetMapping(value = "/get")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getByUserId(UserRecordFer vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        //        List<UserRecordFer> list = userRecordFerService.lambdaQuery().eq(UserRecordFer::getUserId, userId).eq(UserRecordFer::getDeleted, "0").list();
        Page page = Page.of(current, size);
        Page<UserRecordFer> list = userRecordFerService.selectAiPage(vo, page);


        return success(list);
    }
}
